//@login & register
const express = require('express')
const passport = require('passport')
const router = express.Router()
const Profile = require('../../models/Profile')

// $router GET api/profiles/test
// @desc 赶回请求数据的json数据
// @access public

router.get("/test", (req,res) => {
  res.json({
    msg:"profile works"
  })
})

// $router POST api/profiles/add
// @desc 创建信息接口
// @access private

router.post("/add", passport.authenticate("jwt", {session: false}), (req, res) => {
  const profileFields = {}
  if (req.body.type) profileFields.type = req.body.type
  if (req.body.desc) profileFields.desc = req.body.desc
  if (req.body.income) profileFields.income = req.body.income
  if (req.body.expend) profileFields.expend = req.body.expend
  if (req.body.cash) profileFields.cash = req.body.cash
  if (req.body.remark) profileFields.remark = req.body.remark

  new Profile(profileFields).save().then(profile => {
    res.json(profile)
  })
})

// $router GET api/profiles
// @desc 获取所有信息
// @access private

router.get("/", passport.authenticate("jwt", {session: false}), (req, res) => {
  Profile.find()
    .then(profile => {
      if (!profile) {
        return res.status(404).json("no content")
      }
      res.json(profile)
    })
    .catch(err => res.status(404).json(err))
})

// $router GET api/profiles/:id
// @desc 获取单个信息
// @access private

router.get("/:id", passport.authenticate("jwt", {session: false}), (req, res) => {
  Profile.findOne({_id:req.params.id})
    .then(profile => {
      if (!profile) {
        return res.status(404).json("no content")
      }
      res.json(profile)
    })
    .catch(err => res.status(404).json(err))
})

// $router POST api/profiles/edit
// @desc 编辑信息接口
// @access private

router.post("/edit/:id", passport.authenticate("jwt", {session: false}), (req, res) => {
  const profileFields = {}

  if (req.body.type) profileFields.type = req.body.type
  if (req.body.desc) profileFields.desc = req.body.desc
  if (req.body.income) profileFields.income = req.body.income
  if (req.body.expend) profileFields.expend = req.body.expend
  if (req.body.cash) profileFields.cash = req.body.cash
  if (req.body.remark) profileFields.remark = req.body.remark

  Profile.findOneAndUpdate(
    {_id: req.params.id},
    {$set:profileFields},
    {new:true}
    ).then(profile => res.json(profile))

})

// $router POST api/profiles/del/:id
// @desc 删除信息接口
// @access private

router.delete("/del/:id", passport.authenticate("jwt", {session: false}), (req, res) => {
  Profile.findOneAndRemove({_id:req.params.id}).then(profile => {
    profile.save().then(profile => res.json(profile))
  }).catch(err => res.status(404).json('del fail'))
    
})

module.exports = router